<?php

namespace app\common\model\xilujob;

use think\Db;
use think\Exception;
use think\Log;
use think\Model;

class JobRefreshLog extends Model
{
    protected $name = 'xilujob_job_refresh_log';
    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = false;

    /**
     * 职位刷新日志
     * @param int $userId
     * @param int $companyId
     * @param int $jobId
     * @return Object
     */
    public static function refreshJob($userId, $jobId,$companyId) {
        if(!$userId || !$jobId || !$companyId) {
           return __('invalid arguments');
        }
        Db::startTrans();
        try {
            $record = new static();
            $record->save(['user_id'=>$userId,'job_id'=>$jobId,'company_id'=>$companyId]);
            Job::where('id',$jobId)->failException(true)->update(['refresh_count'=>Db::raw('refresh_count+1')]);
            $job = Job::where('id',$jobId)->field('id,publisher_id')->find();
            CompanyStatistics::where('company_id', $job->publisher_id)->update(['job_refresh_count' => Db::raw('job_refresh_count+1')]);
        }catch (\Exception $e) {
            Log::error($e->getMessage()."\n".$e->getTraceAsString());
            throw $e;
        }
        Db::commit();
        return $record;
    }

}
